//
//  IdCardUploadContentView.swift
//  身份证+人脸 开始认证 内容
//
//  Created by 千策万划-mini on 2025/5/2.
//

import Foundation

class IdCardUploadContentView: UIView {
    
    var blockClickUpload:(() -> Void)?
    
    // MARK: - lazy
    lazy var imageScheduleBg: UIImageView = {
        let result = UIImageView()
        result.image = UIImage(named: "icon_auth_schedule_bg")
        result.widthHeight = XY(SCREEN_WIDTH-W(15)*2, W(68))
        return result
    }()
    lazy var imageSchedule: UIImageView = {
        let result = UIImageView()
        result.widthHeight = XY(W(329), W(38))
        return result
    }()
    lazy var imageCardBg: UIButton = {
        let result = UIButton()
        result.widthHeight = XY(W(345), W(304))
        result.setBackgroundImage(UIImage(named: "icon_face_bg"), for: .normal)
        result.tag = 1
        result.addTarget(self, action: #selector(btnClick(_:)), for: .touchUpInside)
        result.isUserInteractionEnabled = true
        return result
    }()
    lazy var imageCardMark: UIImageView = {
        let result = UIImageView()
        result.widthHeight = XY(W(40), W(40))
        result.image = UIImage(named: "icon_face_mark")
        return result
    }()
    lazy var labelCardTitle: OutlineLabel = {
        let result = OutlineLabel()
        return result
    }()
    lazy var imageCardLogo: UIImageView = {
        let result = UIImageView()
        result.widthHeight = XY(W(228), W(151))
        result.image = UIImage(named: "icon_idcard_logo")
        return result
    }()
    lazy var btnCardUpload: UIButton = {
        let result = UIButton()
        result.widthHeight = XY(W(134), W(46))
        result.setBackgroundImage(UIImage(named: "icon_pop_confirm"), for: .normal)
        let label = OutlineLabel()
        label.resetView(
            title: "Click Upload",
            textColor: COLOR_WHITE(),
            font: UIFont.customBoldFont(size: 14),
            shadowColor: UIColor(hex: "#2D0084"),
            outlineColor: UIColor(hex: "#2D0084")
        )
        label.centerXCenterY = XY(result.width/2, result.height/2)
        result.addSubview(label)
        result.tag = 1
        result.addTarget(self, action: #selector(btnClick(_:)), for: .touchUpInside)
        return result
    }()
    lazy var imageFaceBg: UIButton = {
        let result = UIButton()
        result.widthHeight = XY(W(345), W(304))
        result.setBackgroundImage(UIImage(named: "icon_face_bg"), for: .normal)
        result.isUserInteractionEnabled = true
        result.tag = 2
        result.addTarget(self, action: #selector(btnClick(_:)), for: .touchUpInside)
        return result
    }()
    lazy var imageFaceMark: UIImageView = {
        let result = UIImageView()
        result.widthHeight = XY(W(40), W(40))
        result.image = UIImage(named: "icon_face_mark")
        return result
    }()
    lazy var labelFaceTitle: OutlineLabel = {
        let result = OutlineLabel()
        result.resetView(
            title: "Face",
            textColor: COLOR_WHITE(),
            font: UIFont.customExtraBoldFont(size: 20),
            shadowColor: UIColor(hex: "#4105A0"),
            outlineColor: UIColor(hex: "#3B0B9A")
        )
        return result
    }()
    lazy var imageFaceLogo: UIImageView = {
        let result = UIImageView()
        result.widthHeight = XY(W(162), W(151))
        result.image = UIImage(named: "icon_face_logo")
        return result
    }()
    lazy var btnFaceUpload: UIButton = {
        let result = UIButton()
        result.widthHeight = XY(W(134), W(46))
        result.setBackgroundImage(UIImage(named: "icon_pop_confirm"), for: .normal)
        let label = OutlineLabel()
        label.resetView(
            title: "Click Upload",
            textColor: COLOR_WHITE(),
            font: UIFont.customBoldFont(size: 14),
            shadowColor: UIColor(hex: "#2D0084"),
            outlineColor: UIColor(hex: "#2D0084")
        )
        label.centerXCenterY = XY(result.width/2, result.height/2)
        result.addSubview(label)
        result.tag = 2
        result.addTarget(self, action: #selector(btnClick(_:)), for: .touchUpInside)
        return result
    }()
    
    // MARK: - init
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.widthHeight = XY(SCREEN_WIDTH, 0)
        self.backgroundColor = .clear
        self.addSubView()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - 添加
    func addSubView() {
        addSubview(imageScheduleBg)
        imageScheduleBg.addSubview(imageSchedule)
        
        addSubview(imageCardBg)
        imageCardBg.addSubview(imageCardMark)
        imageCardBg.addSubview(labelCardTitle)
        imageCardBg.addSubview(imageCardLogo)
        imageCardBg.addSubview(btnCardUpload)
        
        addSubview(imageFaceBg)
        imageFaceBg.addSubview(imageFaceMark)
        imageFaceBg.addSubview(labelFaceTitle)
        imageFaceBg.addSubview(imageFaceLogo)
        imageFaceBg.addSubview(btnFaceUpload)
        
        resetView()
    }
    
    
    // MARK: - 点击事件
    @objc func btnClick(_ sender: UIControl) {
        feedback()
        sender.addClickAnimate()
        switch sender.tag {
        case 1:
            if let block = blockClickUpload {
                block()
            }
            break
        case 2:
            if let block = blockClickUpload {
                block()
            }
            break
        default:
            break
        }
    }
    
    // MARK: - 重置界面
    func resetView(title: String = "") {
        imageScheduleBg.centerXTop = XY(SCREEN_WIDTH/2, W(16))
        
        let count = GlobalMethod.readInt(forKey: LOCAL_AUTH_PRODUCT_COUNT)
        imageSchedule.image = UIImage(named: count == 5 ? "icon_auth_schedule_1_5" : "icon_auth_schedule_1_4")
        imageSchedule.centerXCenterY = XY(imageScheduleBg.width/2, imageScheduleBg.height/2)
        
        imageCardBg.centerXTop = XY(SCREEN_WIDTH/2, imageScheduleBg.bottom+W(16))
        
        imageCardMark.leftTop = XY(W(20), W(14))
        labelCardTitle.resetView(
            title: title,
            textColor: COLOR_WHITE(),
            font: UIFont.customExtraBoldFont(size: 20),
            shadowColor: UIColor(hex: "#4105A0"),
            outlineColor: UIColor(hex: "#3B0B9A")
        )
        labelCardTitle.centerXCenterY = XY(imageCardBg.width/2, imageCardMark.centerY)
        
        imageCardLogo.centerXTop = XY(imageCardBg.width/2, W(78))
        
        btnCardUpload.centerXTop = XY(imageCardBg.width/2, imageCardLogo.bottom+W(12))
        
        imageFaceBg.centerXTop = XY(SCREEN_WIDTH/2, imageCardBg.bottom+W(16))
        
        imageFaceMark.leftTop = XY(W(20), W(14))
        labelFaceTitle.centerXCenterY = XY(imageFaceBg.width/2, imageFaceMark.centerY)
        
        imageFaceLogo.centerXTop = XY(imageFaceBg.width/2, W(78))
        
        btnFaceUpload.centerXTop = XY(imageFaceBg.width/2, imageFaceLogo.bottom+W(12))
        
        height = imageFaceBg.bottom+getBottomSafeAreaHeight()+60
    }
}
